<!DOCTYPE html>
<script src='vendor/three.js/build/three.min.js'></script>
<script src='vendor/three.js/examples/js/modifiers/SubdivisionModifier.js'></script>

<script src='../threex.dilategeometry.js'></script>
<script src='../threex.atmospherematerial.js'></script>
<!-- include for threex.glowdatgui -->
<script src='vendor/three.js/examples/js/libs/dat.gui.min.js'></script>
<script src="../threex.atmospherematerialdatgui.js"></script>

<body style='margin: 0px; background-color: #000000; overflow: hidden;'><script>
	var renderer	= new THREE.WebGLRenderer();
	renderer.setSize( window.innerWidth, window.innerHeight );
	document.body.appendChild( renderer.domElement );

	var updateFcts	= [];
	var scene	= new THREE.Scene();
	var camera	= new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 100 );
	camera.position.z = 3;

	//////////////////////////////////////////////////////////////////////////////////
	//		build original object						//
	//////////////////////////////////////////////////////////////////////////////////

	var geometry	= new THREE.TorusKnotGeometry(1-0.25, 0.25, 32*3, 32);
	var material	= new THREE.MeshNormalMaterial();
	var mesh	= new THREE.Mesh( geometry, material );
	// mesh.visible	= false
	scene.add( mesh );
	
	//////////////////////////////////////////////////////////////////////////////////
	//		put the hallo around						//
	//////////////////////////////////////////////////////////////////////////////////

	// create the mesh for the halo with AtmosphereMaterial
	var geometry	= mesh.geometry.clone()	
	THREEx.dilateGeometry(geometry, 0.15)
	var material	= THREEx.createAtmosphereMaterial()
	var meshHalo	= new THREE.Mesh(geometry, material );	
	scene.add( meshHalo );

	// possible customisation of AtmosphereMaterial
	material.uniforms.glowColor.value	= new THREE.Color('cyan')
	material.uniforms.coeficient.value	= 1.1
	material.uniforms.power.value		= 1.4
	
	// here is how to setup a datGUI for fine tuning
	var datGUI	= new dat.GUI()
	new THREEx.addAtmosphereMaterial2DatGui(material, datGUI)

	//////////////////////////////////////////////////////////////////////////////////
	//		Camera Controls							//
	//////////////////////////////////////////////////////////////////////////////////
	var mouse	= {x : 0, y : 0}
	document.addEventListener('mousemove', function(event){
		mouse.x	= (event.clientX / window.innerWidth ) - 0.5
		mouse.y	= (event.clientY / window.innerHeight) - 0.5
	}, false)
	updateFcts.push(function(delta, now){
		camera.position.x += (mouse.x*25 - camera.position.x) * (delta*3)
		camera.position.y += (mouse.y*25 - camera.position.y) * (delta*3)
		camera.lookAt( scene.position )
	})


	//////////////////////////////////////////////////////////////////////////////////
	//		render the scene						//
	//////////////////////////////////////////////////////////////////////////////////
	updateFcts.push(function(){
		renderer.render( scene, camera );		
	})
	
	//////////////////////////////////////////////////////////////////////////////////
	//		loop runner							//
	//////////////////////////////////////////////////////////////////////////////////
	var lastTimeMsec= null
	requestAnimationFrame(function animate(nowMsec){
		// keep looping
		requestAnimationFrame( animate );
		// measure time
		lastTimeMsec	= lastTimeMsec || nowMsec-1000/60
		var deltaMsec	= Math.min(200, nowMsec - lastTimeMsec)
		lastTimeMsec	= nowMsec
		// call each update function
		updateFcts.forEach(function(updateFn){
			updateFn(deltaMsec/1000, nowMsec/1000)
		})
	})
</script></body>
